<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /*
        手写apply方法
        1.定义函数
        2.this并调用原函数
        3.接受剩余参数并返回结果
        4.调用Symbol调优
        */
        //1.定义函数
        Function.prototype.myApply = function (context, args) {
            let key = Symbol('key')
            context[key] = this
            const res = context[key](...args)
            delete context[key]//
            return res
        }
        function fn(name, age) {
            console.log(this)
            console.log(name, age)
            return name + age
        }
        const person = {
            name: 'why'
        }
        const res = fn.myApply(person, [20, 18])
        console.log('--返回值为--' + res)

    </script>
</body>